<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class Distribute
{
    protected $id;

    public function __construct($uid, $title = '')
    {
        $this->id = $uid;
        $this->title = $title;
    }

    protected function script()
    {
        return <<<SCRIPT
 layui.use('layer', function(){
        var layer = layui.layer;
        $('.distribute').click(function () {
            var that = this;
            var uid = $(that).attr('data-uid');
            var title = $(that).attr('data-title');
           
             var index = layer.open({
                content: '/admin/get/all/admin',
                type: 2,
                title: '<\span style="color:red">'+title+'</\span>',
                area: ['500px', '300px'],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var adminid = $(layero).find("iframe").contents().find("#dis").val();
                  
                    $.ajax({
                        url: '/admin/get/all/admin?uid='+uid+'&adminid='+adminid,
                        type: 'post',
                        success: function (res) {
                            if(res.code) {
                                layer.msg(res.text)
                                layer.close(index);
                                return;
                            }
                            layer.msg(res.text)
                            layer.close(index);
                            location.href = location.href
                        }
                    })
                }
            });
        });
 });

    
SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return "&nbsp;<a href='javascript:void(0);'><button data-uid='{$this->id}'  data-title='{$this->title}' class='distribute layui-btn  layui-btn-normal layui-btn-xs'>分配</button></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}